package com.yk.downtest.compass;

import com.yk.filedownload.entity.DataCarrier;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import sun.security.util.Length;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.Deflater;

/**
 * Project Name:cykfiledownload
 * File Name:testcompass
 * Package Name:com.yk.downtest.compass
 * Date:2017\12\6 0006
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */
public class testcompass {
    @Test
    public void testCom() throws Exception{
//        File file = new File("H:/authcode/example/douban/waiting.jpg");
//        InputStream in = new FileInputStream(file);
//        byte[] mateData = null;
//        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
//        byte[] buff = new byte[100];
//        int rc = 0;
//        while ((rc = in.read(buff, 0, 100)) > 0) {
//            swapStream.write(buff, 0, rc);
//        }
//        mateData = swapStream.toByteArray();

        String url = "http://p3.pstatp.com/origin/1dbf0000d92bde896973";

        URL url_now = new URL(url);


        HttpURLConnection conn = (HttpURLConnection) url_now.openConnection();

        System.out.println(conn.getURL().toString());
        conn.setRequestMethod("GET");

//        conn.setRequestProperty("Range","bytes=0-1023");

        InputStream in = conn.getInputStream();

        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int rc = 0;
        while ((rc = in.read(buff, 0, 1024)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] mateData = swapStream.toByteArray();



        DataCarrier carrier  = new DataCarrier();
        carrier.CompressData(mateData);
        byte[] data = carrier.UncompressData();

        File filedown = new File("H:/test/test11.png");
        FileOutputStream fileOutputStream = new FileOutputStream(filedown);
        fileOutputStream.write(data);
        fileOutputStream.close();
//        DataCompass.Compass.Builder builder = DataCompass.Compass.newBuilder();
//        builder.setInfo("test");
//        builder.setDataCell(ByteString.copyFrom(mateData));
//        DataCompass.Compass compass = builder.build();
//        System.out.println(compass.toString());
//        System.out.println(compass.toString().length());


//        byte[] show1 = null;
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        GZIPOutputStream gzip = new GZIPOutputStream(bos);
//        gzip.write(mateData);
//        gzip.finish();
//        gzip.close();
//        show1 = bos.toByteArray();
//        bos.close();
//
//
//        String show1Str = new String(show1,"ISO-8859-1");
//        System.out.println(show1Str);
//        System.out.println(show1Str.length());
//
//        byte[] trans = show1Str.getBytes("ISO-8859-1");
//        System.out.println(trans.length);

//        byte[] b = null;
//        ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
//        ZipOutputStream zip = new ZipOutputStream(bos2);
//        ZipEntry entry = new ZipEntry("zip");
//        entry.setSize(mateData.length);
//        zip.putNextEntry(entry);
//        zip.write(mateData);
//        zip.closeEntry();
//        zip.close();
//        b = bos2.toByteArray();
//        bos2.close();
//        System.out.println(b.length);

//        byte[] comappsdata;
//        int len = 0;
//        Deflater defl = new Deflater(8);
//        defl.setInput(mateData);
//        defl.finish();
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        byte[] outputByte = new byte[1024];
//        try {
//            while (!defl.finished()) {
//                // 压缩并将压缩后的内容输出到字节输出流bos中
//                len = defl.deflate(outputByte);
//                bos.write(outputByte, 0, len);
//            }
//            defl.end();
//         } finally {
//             bos.close();
//         }
//        comappsdata = bos.toByteArray();
//        System.out.println(comappsdata.length);
//        String str = Base64.encodeBase64String(comappsdata);
//        System.out.println(str);
//        System.out.println(str.length());
//        String show1Str = new String(comappsdata,"ISO-8859-1");
//        System.out.println(show1Str);
//        System.out.println(show1Str.length());

    }


}
